package com.ruoyi.util;

import com.ruoyi.appuser.domain.Appuser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

/**
 * @author mjl
 */
@Component
public class AuthUtil {

    private static AuthUtil authUtil = new AuthUtil();

    @Autowired
    private RedisCache redisCache;

    @PostConstruct
    public void init(){
        authUtil.redisCache = redisCache;
    }

    public static Appuser getAppLoginUser(){
        HttpServletRequest request = WebUtil.getRequest();
        if (request == null) {
            return null;
        }
        String token = request.getHeader("token");
        Appuser appuser = authUtil.redisCache.getCacheObject(token);
        return appuser;
    }

    public static String getAppUserId(){
        try {
            String id = getAppLoginUser().getId();
            return String.valueOf(id);
        } catch (Exception e){
            return null;
        }
    }
}
